<?php
namespace app\admin\controller;
use think\Db;
use app\admin\model\Base;
use app\common\controller\order\Orderget;
class Xitong extends Common
{
//	短信模板页面
	public function duanxin(){
		return $this->fetch();
	}
	//短息列表数据
    public function get_list_duanxin()
    {	
		$page = input('page');
		$limit = input('limit');
		$TrueName= input('TrueName');//真实姓名
		$UserName= input('UserName');//用户名
		$RoleID=input('RoleID');//角色ID
	    $where=[];
		if($TrueName){
			$where['TrueName'] = $TrueName;
		}
		if($UserName){
			$where['UserName'] = $UserName;
		}
		if($RoleID){
			$where['RoleID'] = $RoleID;
		}
        $data=Db::table('xb_sys_smstemplates')->page($page,$limit)->where($where)->select();
		
//		p($data);die;
//		foreach ($data as $key => $value) {
//			$data[$key]['money']=Db::table('xb_goods')->where(['ID'=>$value['money']])->value('SalePrice');
//			
//		}
		$cuont=Db::table('xb_sys_smstemplates')->where($where)->count('ID');	
		if($data){
		   songsong($data,$cuont);	
		}
    }
	//短信编辑页面
	public function editss_duanxin(){
		$this->logs('短信编辑');
		$ID= input('ID');
		$data1=Db::table('xb_sys_smstemplates')->where('ID',$ID)->find();
//		p($data1);die;
		$this->assign('data1',$data1);
//		$data=Db::table('xb_goods')->select();
//		$this->assign('data',$data);
//		p($data);
		return $this->fetch();
	}
	
	
	//短息编辑操作
	public function edit_duanxin(){
		
//		p($_POST);die;
        $this->logs('短信编辑');
		$data=$_POST;
		$data['UpdateTime']=date('Y-m-d H:i:s');//审核时间
//		p($data);die;
		$ID=Db::table('xb_sys_smstemplates')->update($data);
		if($ID){
	    show_success();
		}else{
			show_error();
		}
		
	}
	//	代扣
	public function daikou(){
		return $this->fetch();
	}
	//代扣列表
    public function daikou_list()
    {	
		$page = input('page');
		$limit = input('limit');
		$Orderget=new Orderget();
		$whereyuanshi=$Orderget->sorch();
		if(!empty($whereyuanshi['a.OrderSn'])){
			$where['OrderSn']=$whereyuanshi['a.OrderSn'];
		}else{
			$where=[];
		}
        $data=Db::table('xb_daikou')->page($page,$limit)->where($where)->order('ID desc')->select();
		foreach ($data as $key => $value) {
			$data[$key]['userId']=Db::table('xb_mem_info')->where(['ID'=>$value['userId']])->value('TrueName');
			if($value['type']=='1'){
				
			  $data[$key]['type']='<span style="color:blue">到期还款</span>';
			}
			if($value['type']=='2'){
				 $data[$key]['type']='<span style="color:green">到期展期</span>';
			}
			if($value['type']=='3'){
				
			  $data[$key]['type']='<span style="color:red">逾期还款</span>';
			}
			if($value['type']=='4'){
				 $data[$key]['type']='<span style="color:yellor">逾期展期</span>';
			}
			if($value['is_zidong']=='1'){
				
			  $data[$key]['is_zidong']='<span style="color:red;">自动代扣</span>';
			}
			if($value['is_zidong']=='2'){
				 $data[$key]['is_zidong']='<span style="color:green;">手动代扣</span>';
			}
			
		}
//		p($data);die;
		$cuont=Db::table('xb_daikou')->where($where)->count('ID');	
		if($data){
		   songsong($data,$cuont);	
		}else{
			 songsong();	
		}
    }
	
//	协议列表 
	public function xieyi(){
		return $this->fetch();
	}
	//协议列表
    public function get_list_xieyi()
    {	
		$page = input('page');
		$limit = input('limit');
		$TrueName= input('TrueName');//真实姓名
		$UserName= input('UserName');//用户名
		$RoleID=input('RoleID');//角色ID
	    $where=[];
		if($TrueName){
			$where['TrueName'] = $TrueName;
		}
		if($UserName){
			$where['UserName'] = $UserName;
		}
		if($RoleID){
			$where['RoleID'] = $RoleID;
		}
        $data=Db::table('xb_sys_content_change')->page($page,$limit)->where($where)->select();
//		foreach ($data as $key => $value) {
//			$data[$key]['money']=Db::table('xb_goods')->where(['ID'=>$value['money']])->value('SalePrice');
//			
//		}
		$cuont=Db::table('xb_sys_content_change')->where($where)->count('ID');	
		if($data){
		   songsong($data,$cuont);	
		}
    }
	
//协议编辑页面
	public function editss_xieyi(){
		$this->logs('协议编辑');
		$ID= input('ID');
		$data1=Db::table('xb_sys_content_change')->where('ID',$ID)->find();
//		p($data1);die;
		$this->assign('data1',$data1);
		$data=Db::table('xb_goods')->select();
		$this->assign('data',$data);
//		p($data);
		return $this->fetch();
	}
	
	//协议编辑操作
	public function edit_xieyi(){
         $this->logs('编辑协议');
		$data=$_POST;
		$data['UpdateTime']=date('Y-m-d H:i:s');//审核时间
//		p($data);die;
		$ID=Db::table('xb_sys_content_change')->update($data);
		if($ID){
	    show_success();
		}else{
			show_error();
		}
		
	}
	
	
	
	
	
//	提额管理
	public function tier(){
	
		return $this->fetch();
	}
	//	提额管理列表
    public function get_list_tier()
    {	
		$page = input('page');
		$limit = input('limit');
		$TrueName= input('TrueName');//真实姓名
		$UserName= input('UserName');//用户名
		$RoleID=input('RoleID');//角色ID
	    $where=[];
		if($TrueName){
			$where['TrueName'] = $TrueName;
		}
		if($UserName){
			$where['UserName'] = $UserName;
		}
		if($RoleID){
			$where['RoleID'] = $RoleID;
		}
        $data=Db::table('xb_sys_tier')->page($page,$limit)->order('ID desc')->where($where)->select();
		foreach ($data as $key => $value) {
			$data[$key]['money']=Db::table('xb_goods')->where(['ID'=>$value['money']])->value('SalePrice');
			
		}
		$cuont=Db::table('xb_sys_tier')->where($where)->count('ID');	
		if($data){
		   songsong($data,$cuont);	
		}
    }
	//	提额管理添加页面
	public function add_tier(){
	
	    $data=Db::table('xb_goods')->select();
		$this->assign('data',$data);
		return $this->fetch();	
	}
	//	提额管理添加操作
	public function add_tier_true(){
		
		$this->addOperation('提额管理添加');
		$data=$_POST;
		$data['CreateTime']=date('Y-m-d H:i:s');//审核时间
//		p($data);die;
		$ID=Db::table('xb_sys_tier')->insertGetId($data);
		if($ID){
	    show_success();
		}else{
			show_error();
		}
	}
	
		//	授信额度编辑页面
	public function editss_tier(){
		$ID= input('ID');
		$data1=Db::table('xb_sys_tier')->where('ID',$ID)->find();
		
//		p($data1);die;
		$this->assign('data1',$data1);
		$data=Db::table('xb_goods')->select();
		$this->assign('data',$data);
//		p($data);
		return $this->fetch();
	}
	//	授信额度编辑操作
	public function edit_tier(){
		$this->logs('提额编辑');
		$data=$_POST;
		$data['UpdateTime']=date('Y-m-d H:i:s');//审核时间
//		p($data);die;
		$ID=Db::table('xb_sys_tier')->update($data);
		if($ID){
	    show_success();
		}else{
			show_error();
		}
		
	}
//	
	//	贷款产品 页面
	public function chanpin(){
		$data=Db::table('xb_sys_basicinfo')->find();
//		p($data);
		$this->assign('data',$data);
		return $this->fetch();
	}
	//	贷款产品数据 类表
    public function get_list_chanpin()
    {	
		$page = input('page');
		$limit = input('limit');
		$TrueName= input('TrueName');//真实姓名
		$UserName= input('UserName');//用户名
		$RoleID=input('RoleID');//角色ID
	    $where=[];
		if($TrueName){
			$where['TrueName'] = $TrueName;
		}
		if($UserName){
			$where['UserName'] = $UserName;
		}
		if($RoleID){
			$where['RoleID'] = $RoleID;
		}
        $data=Db::table('xb_goods')->page($page,$limit)->order('ID desc')->where($where)->select();
		$cuont=Db::table('xb_goods')->where($where)->count('ID');	
		foreach ($data as $k => $v) {
			if($v['IsShelves']=='0'){
				$v['IsShelves']='<span style="color:red;">未上架</span>';
			}
			if($v['IsShelves']=='1'){
					$v['IsShelves']='<span style="color:green;">已上架</span>';

			}
			
			$data[$k]=$v;
		}
		foreach ($data as $k => $v) {
			if($v['Statusyuqi']=='0'){
				$v['Statusyuqi']='<span style="color:red;">关闭</span>';
			}
			if($v['Statusyuqi']=='1'){
				$v['Statusyuqi']='<span style="color:green;">开启</span>';
			}
			if($v['Status']=='0'){
				$v['Status']='<span style="color:red;">关闭</span>';
			}
			if($v['Status']=='1'){
				$v['Status']='<span style="color:green;">开启</span>';
			}
			if($v['defont']=='1'){
					$v['defont']='<span style="color:blue;">默认</span>';

			}
			if($v['defont']=='2'){
					$v['defont']='<span style="color:green;">正常</span>';

			}
			$data[$k]=$v;
		}
		
//		p($data);die;
		if($data){
		   songsong($data,$cuont);	
		}
    }
//	贷款产品添加页面
	public function adminform(){
		
		
		return $this->fetch();	
	}
	
//	贷款产品添加操作
	public function add_chanpin(){
		$this->logs('添加产品');
		$data=$_POST;
		
		//		产品服务费  1先收取 2后收取
		if(!empty($data['fuwufeikaiguan']) && $data['fuwufeikaiguan']=='on'){
			$data['fuwufeikaiguan']='1';
		}else{
			$data['fuwufeikaiguan']='2';
		}
//		正常客户展期开关
		if(!empty($data['Status']) && $data['Status']=='on'){
			$data['Status']='1';
		}else{
			$data['Status']='0';
		}
//		正常客户展期开关
		if(!empty($data['Statusyuqi']) && $data['Statusyuqi']=='on'){
			$data['Statusyuqi']='1';
		}else{
			$data['Statusyuqi']='0';
		}
		$data['CreateTime']=date('Y-m-d H:i:s');//时间
//		p($data);die;
		$ID=Db::table('xb_goods')->insertGetId($data);
		if($ID){
	    show_success();
		}else{
			show_error();
		}
		
	}
	
	//	贷款产品编辑页面
	public function editss_chanpin(){
		
		$ID= input('ID');
		$data=Db::table('xb_goods')->where('ID',$ID)->find();
		$this->assign('data',$data);
//		p($data);
		return $this->fetch();
	}
	//	贷款产品编辑操作
	public function edit_chanpin(){
		$this->logs('产品编辑');
		$data=$_POST;
//	p($data);die;
		if(!empty($data['Statusyuqi']) && $data['Statusyuqi']=='on'){
			$data['Statusyuqi']='1';
		}else{
			$data['Statusyuqi']='0';
		}
		if(!empty($data['Status']) && $data['Status']=='on'){
			$data['Status']='1';
		}else{
			$data['Status']='0';
		}
		
		if(!empty($data['IsShelves']) && $data['IsShelves']=='on'){
			$data['IsShelves']='1';
		}else{
			$data['IsShelves']='0';
		}
//		产品服务费  1先收取 2后收取
		if(!empty($data['fuwufeikaiguan']) && $data['fuwufeikaiguan']=='on'){
			$data['fuwufeikaiguan']='1';
		}else{
			$data['fuwufeikaiguan']='2';
		}
//		产品类型  1默认 2正常
		if(!empty($data['defont']) && $data['defont']=='on'){
			$data['defont']='2';
			
		}else{
			$data['defont']='1';
		}
		$data['CreateTime']=date('Y-m-d H:i:s');//审核时间
		if($data['defont']=='1'){
		Db::table('xb_goods')->where(['defont'=>'1'])->update(['defont'=>'2']);
		}
		$ID=Db::table('xb_goods')->update($data);
		if($ID){
	    show_success();
		}else{
			show_error();
		}
		
	}
	
	
	
	
	
	
	
	
	
	
	
//   版本管理页面
    public function banben()
    {	
		return $this->fetch();	
    }
	
//	版本管理数据
    public function get_list_banben()
    {
		$where=[];
		$page = input('page');
		$limit = input('limit');
		$re=Db::table('xb_version')->page($page,$limit)->where($where)->select();
		foreach ($re as $k => $v) {
			if($v['isForced']=='1'){
				$v['isForced']='<span style="color:red;">是</span>';
			}
			if($v['isForced']=='2'){
					$v['isForced']='<span style="color:green;">否</span>';

			}
			
			$re[$k]=$v;
		}
		$cuont=Db::table('xb_version')->count('ID');
		if($re){
		   songsong($re,$cuont);
		}
    }
		
//	版本管理添加页面
		public function add_banben(){
		return $this->fetch();
	}
		
	//	版本添加操作
	public function add_banben_caozuo(){
		$this->logs('添加版本');
		$data=$_POST;
		if(!empty($data['isForced']) && $data['isForced']=='on'){
			$data['isForced']='1';
		}else{
			$data['isForced']='2';
		}
		$data['CreateTime']=date('Y-m-d H:i:s');//审核时间
//		p($data);die;
		$ID=Db::table('xb_version')->insertGetId($data);
		if($ID){
	    show_success();
		}else{
			show_error();
		}
		
	}
//	版本管理编辑页面
		public function editss_banben(){
		$ID= input('ID');
		$data=Db::table('xb_version')->where('ID',$ID)->find();
		$this->assign('data',$data);
//		p($data);die;
		return $this->fetch();
	}
	//	版本管理编辑操作
	public function edit_banben(){
		$this->logs('版本编辑');
		$data= input();
		$data['UpdateTime']=date('Y-m-d H:i:s');//时间
		
		
		if(!empty($data['isForced']) && $data['isForced']=='on'){
			$data['isForced']='1';
		}else{
			$data['isForced']='2';
		}
		$re=Db::table('xb_version')->update($data);
		if($re){
	    show_success();
		}else{
			show_error();
		}	
	}
	
//	系统配置页面
	public function index(){
		$data=Db::table('xb_sys_basicinfo')->find();
//		p($data);
		$this->assign('data',$data);
		return $this->fetch();
	}
	//	系统配置编辑
	public function edit_index(){
		$data= input('post.');
		$data['UpdateTime']=date('Y-m-d H:i:s');//时间
        $RoleId = session('info.RoleID');
        if ($RoleId == 1){
            $key = [
                'order',
                'cuishouren',
                'daikou',
                'Payceshi',
                'zdfk',
                'isAlipay',
                'isWx',
                'isBank',
                'fengkong_kaiguan',
                'fengkong_mopan_kaiguan',
                'BalanceIsCheck'
            ];
            foreach ($key as $v){
                if (isset($data[$v])) {
                    $data[$v] = '1';
                }else{
                    $data[$v] = '2';
                }
            }
        }
		
		$basicinfo=Db::table('xb_sys_basicinfo')->find();
		unset($basicinfo['repayment_time']);
		unset($basicinfo['UpdateTime']);
		$str='';
		foreach ($data as $k => $v) {
			if(isset($basicinfo[$k]) && $basicinfo[$k]!=$v){
				$str.=$k.':修改前:'.$basicinfo[$k].'修改后:'.$v."<br>";
			}
		}
		$this->logs('系统配置编辑:<br>'.$str);
		
		
		
		$re=Db::table('xb_sys_basicinfo')->update($data);
		if($re){
		    //清空系统缓存
            cache('systemAllData',NULL);
	        show_success();
		}else{
			show_error();
		}	
	}
//	对接配置页面
   public function duijie(){
		$data=Db::table('xb_sys_basicinfo')->find();
		$this->assign('data',$data);
		return $this->fetch();
	}
// 对接配置数据
    public function get_list_duijie(){
    	
		$where=[];
		$page = input('page');
		$limit = input('limit');
		$re=Db::table('xb_sys_integrate')->page($page,$limit)->where($where)->select();
		$cuont=Db::table('xb_sys_integrate')->count('ID');
		if($re){
		   songsong($re,$cuont);
		}
    }

	//	对接配置编辑页面
    public function editss_duijie(){
		$ID= input('ID');
		$data=Db::table('xb_sys_integrate')->where('ID',$ID)->find();
		$this->assign('data',$data);
//		p($data);
		return $this->fetch();
	}
	//	对接配置类表
	public function edit_duijie(){
	
		$data= input();
		$data['UpdateTime']=date('Y-m-d H:i:s');//时间
		$re=Db::table('xb_sys_integrate')->update($data);
		if($re){
            //清除产品缓存信息
            cache(input('EName'),NULL);
	        show_success();
		}else{
			show_error();
		}	
	}
	//	对接配置配置页面
		public function peizhiss_duijie(){
		$ID= input('ID');
		$data=Db::table('xb_sys_inteparameter')->where('IntegrateID',$ID)->select();
		$this->assign('data',$data);
		return $this->fetch();
	}
	//	对接配置配置操作
	public function peizhi_duijie(){
		$this->logs('对接配置编辑');
		$arr= input();
        $data=array_to_array($arr);
		$re=[];
		foreach ($data as $key => $value) {
		  $value['UpdateTime']=date('Y-m-d H:i:s');//时间
		  $re[]=Db::table('xb_sys_inteparameter')->update($value);
		}
		if($re){
	    show_success();
		}else{
			show_error();
		}	
	}


	
	
	
	
	
	
	
	
	
}
